define(["jtable"],function(){
	var createTable = function (){
		$('#authoritTable').jtable({
						title: '角色管理',
						paging: true,
						sorting: true,
						defaultSorting: 'id DESC',
						selectingCheckboxes: true, 
						openChildAsAccordion: true,
						actions: {
							listAction: '../admin/authorit/authorit.json',
							deleteAction: '../admin/authorit/delauthorit.json',
							updateAction: '../admin/authorit/modifyauthorit.json',
							createAction: '../admin/authorit/createauthorit.json'
						},
						fields: {
							id: {
								key: true,
								create: false,
								edit: false,
								list: false
							},
							auName: {
								title: '中文名称',
								width: '40%'
							},
							authority: {
								title: '英文名称',
								width: '40%'
							},
			                res: {
			                    title: '查看资源',
			                    width: '8%',
			                    sorting: false,
			                    edit: false,
			                    create: false,
			                    display: function (data) {
			                    	var childTable = null;
			                        var $img = $('<img src="../images/list_metro.png" style="cursor:pointer" title="查看资源" />');
			                        $img.click(function () {
			                            $('#authoritTable').jtable('openChildTable',$img.closest('tr'),{
			                                    title: "<span style='color:#ff0000'>"+data.record.auName+"</span>"+"的资源列表",
			                                    paging: true,
			            						sorting: true,
			            						defaultSorting: 'id DESC',
			            						selecting: true, 
			            			            multiselect: true, 
			            			            selectingCheckboxes: true,
			            						actions : {
			        								listAction : '../admin/res/'+data.record.id+'/res.json'
			        							},toolbar: {
			        				                items: [{
			        				                    tooltip: '保存设置',
			        				                    text: '保存设置',
			        				                    click: function () {
			        				                    	if(childTable){
			        				                    		var $selectedRows = childTable.jtable('selectedRows');
			        				                    		var ids = [];
			        				                    		$selectedRows.each(function () {
				        				                            ids.push($(this).data('record').id);
				        				                        });
			        				                    		$.ajax({
			        				                    			  type: 'POST',
			        				                    			  url: '../admin/res/'+data.record.id+'/modifyAuthoritAndRes.json',
			        				                    			  data: $.param({'ids':ids}, true),
			        				                    			  dataType: 'html',
			        				                    			  success: function(date){
			        				                    				  if(date=="ok"){
			        				                    					  alert("修改成功！");
			        				                    				  }else{
			        				                    					  alert('修改失败！');
			        				                    				  }
			        				                    				  childTable.jtable('load');
			        				                    			  },
			        				                    			  error:function(){
			        				                    				  alert('修改失败！');
			        				                    			  }
			        				                    		});
			        				                    	}else{
			        				                    		alert('出现错误！');
			        				                    	}
			        				                    }
			        				                }]
			        							},
			                                    fields: {
			                                    	authoritId: {
			                                            type: 'hidden',
			                                            defaultValue: data.record.id
			                                        },
			                                        id : {
			                                        	key: true,
			                                        	title : '资源ID',
			        									width : '40%',
			        									edit: false,
			        					                create: false
			        								},
			        								url : {
			        									title : '资源url',
			        									width : '40%'
			        								}
			                                    },
			            						rowInserted: function (event, data) {
			            							if(data.record.aid){
			            								 $('#authoritTable').jtable('selectRows', data.row);
			            							}
			            			            }
			                                }, function (data) {
			                                	childTable = data.childTable;
			                                    data.childTable.jtable('load');
			                                });
			                        });
			                        return $img;
			                    }
			                }
						}
				});
			$('#authoritTable').jtable('load');
		    $('#LoadRecordsButton').click(function (e) {
		        e.preventDefault();
		        $('#authoritTable').jtable('load', {
		        	auName: $('#auName').val(),
		            authority: $('#authority').val()
		        });
		    });
	};
	return {
		createTable: createTable
	};
});